<?php
/*
Plugin Name: Megatop
Plugin URI: http://vmrf.ru
Description: Show mega top news
Author: vmrf.ru
Version: 1.0
Author URI: http://vmrf.ru
 */

namespace AY\Widget;

class Top extends \AY\Widget\AWidget
{
    protected $widgetName = 'Top';
    protected $widgetOptions = array(
        'classname' => 'top-ay',
        'description'   => 'widget show top news'
        );

    protected function getData() {
        $region = ay()->region()->getCategoryByRegion();
        $cat_not_in = array(141);
        if($region)
            array_push($cat_not_in, $region);

        return query_posts(array('numberposts' => 10, 'category__not_in'=>$cat_not_in ));
    }

    function widget( $args, $instance ) {
        $resentPost = $this->getData();//array('numberposts'=>$instance['count'],'category'=>$this->category));
        ?>
        <div class="column_title clearfix">
            <h3><a href="<?php echo get_site_url().date('/Y/m/') ?>">Новости тематик</a></h3>
            <a href=""><img src="<?php echo get_template_directory_uri(); ?>/assets/images/rss.png" alt="" /></a>
         </div>
        <div style="clear:both"></div>
        <div class="news_list">
        <?php foreach ($resentPost as $post) :setup_postdata($post); ?>
            <div class="item clearfix">
                <a href="<?php echo get_permalink($post->ID); ?>"
                   title="<?php echo htmlspecialchars($post->post_title); ?>">
                <?php
                if ( has_post_thumbnail($post->ID) ) { // check if the post has a Post Thumbnail assigned to it.
                    echo get_the_post_thumbnail($post->ID, array(75,75));
                } else {
                    echo '<img src="'. get_template_directory_uri().'/assets/images/vm_tumb.jpg" alt="" />';
                }?>
                </a>
                <a href="<?php echo get_permalink($post->ID); ?>"
                   title="<?php echo htmlspecialchars($post->post_title); ?>"><?php echo $post->post_title ?></a><br>
                <?php echo $post->post_excerpt?><a href="<?php echo get_permalink($post->ID); ?>"> Дальше &rarr;</a>
                 <div class="tools">
                    <span class="date"><span><?php echo date("H:i ",strtotime($post->post_date)); ?></span><?php echo date("d/m",strtotime($post->post_date)); ?></span>
                <?php
                    $post_categories = wp_get_post_categories($post->ID);
                    $cats = array();

                    foreach($post_categories as $c){
                        $cat = get_category( $c );
                        echo "<a href='/category/".$cat->slug."' class='category'>".$cat->name." </a>";
                    }
                ?>
                </div>
            </div>
        <?php endforeach; ?>
        <div style="padding-bottom: 10px;" >
            <a href="<?php echo get_site_url().date('/Y/m/') ?>">Остальные события &rarr;</a>
        </div>
        </div><!-- .new_list -->
        <?php
    }

    function form( $instance ) {
    }

    function update($new_instance, $old_instance) {
    }
}
